const express = require("express");
const router = express.Router();

const config = require("../../config/config");
const SettingModel = require("../../model/settingModel");
const { multer, getUnix } = require("../../model/tools");
const settingModel = require("../../model/settingModel");

router.get("/", async (req, res) => {
    const result = await SettingModel.find({});
    res.render("admin/setting/index.html", { list: result[0] });
})

const cpUpload = multer().fields([
    { name: "site_logo", maxCount: 1 },
    { name: "no_picture", maxCount: 1 }
])

router.post("/doEdit", cpUpload, async (req, res) => {
    let json = {};
    if(req.files.site_logo) {
        let site_logo = req.files.site_logo[0].path.substr(7);
        json = Object.assign(json, { site_logo });
    }
    if(req.files.no_picture) {
        let no_picture = req.files.no_picture[0].path.substr(7);
        json = Object.assign(json, { no_picture });
    }
    await settingModel.updateMany({}, Object.assign(json, req.body));
    res.render("admin/public/success.html", {
        "redirectUrl": `/${config.adminPath}/setting`,
        "message": "修改数据成功"
    });
})

module.exports = router